// pages/details/comment_details/comment_details.js
const request = require("../../../utils/request.js")
const util = require("../../../utils/util.js")
const sortMethod = require("../../../model/bubbleSort.js")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    opts: [
      "全部",
      "最新",
      "2星及以下",
      "3星",
      "4星及以上",
      "有图",
      "追加"
    ],
    sortedComments: [],
    comments: [
    ],
    commentsOpt: 0,
    goodId: -1,
    goodName:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var goodId = options.goodId;
    var goodName = options.goodName;
    that.setData({
      goodId:goodId,
      goodName: goodName
    })
    var url = request.URL + '/goodDetail/comment'
    request.request({
      url: url,
      data: {
        goodId: goodId
      },
      success: function (res) {
        var comments = res.data;
        console.log(comments);
        for (var i = 0; i < comments.length; i++) {
          var commentImgArray = comments[i].commentImgArray;
          var commentTime = util.formatTime(parseInt(comments[i].commentTime) / 1000);
          comments[i].commentTime = commentTime;
          console.log(commentImgArray)
          if (commentImgArray == "null") {
            comments[i].imgArr = null;
          } else {
            commentImgArray = commentImgArray.replace(/\[|]|'|"/g, '');
            var commentImgArr = commentImgArray.split(',');
            comments[i].imgArr = commentImgArr;
          }
        }

        that.setData({
          comments: comments,
          sortedComments: comments
        })

      },
      fail: function () {
        request.showFailInfo()
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  /**
   * 点击上面按钮显示相应的评论
   */
  commentsOpts: function (e) {
    var that = this;
    var index = e.target.dataset.index;
    console.log(that.data.comments)
    switch (index) {
      case 0:
        var originalComments = that.data.comments;
        var key = 'commentId';
        var sortedComments = sortMethod.bubbleSort_lowToHigh(originalComments, key)
        that.setData({
          sortedComments: sortedComments,
          commentsOpt: 0
        })
        break;
      case 1:
        var originalComments = that.data.comments;
        var key = 'commentId';
        var sortedComments = sortMethod.bubbleSort_highToLow(originalComments, key)
        that.setData({
          sortedComments: sortedComments,
          commentsOpt: 1
        })
        break;
      case 2:
        var originalComments = that.data.comments;
        var sortedComments = that.getCommentsByLevel(originalComments, 2, 1)
        that.setData({
          sortedComments: sortedComments,
          commentsOpt: 2
        })
        break;
      case 3:
        var originalComments = that.data.comments;
        var sortedComments = that.getCommentsByLevel(originalComments, 3, 3)
        that.setData({
          sortedComments: sortedComments,
          commentsOpt: 3
        })
        break;
      case 4:
        var originalComments = that.data.comments;
        var sortedComments = that.getCommentsByLevel(originalComments, 5, 4)
        that.setData({
          sortedComments: sortedComments,
          commentsOpt: 4
        })
        break;
      case 5:
        var originalComments = that.data.comments;
        var sortedComments = [];
        for (var i = 0; i < originalComments.length; i++) {
          var commentImgArray = originalComments[i].commentImgArray;
          console.log(commentImgArray)
          if (commentImgArray != 'null' && commentImgArray != '[]' && commentImgArray != '') {
            sortedComments.push(originalComments[i]);
          } else {
            continue;
          }
        }
        that.setData({
          sortedComments: sortedComments,
          commentsOpt: 5
        })
        break;
      case 6:
        break;
    }
  },
  getCommentsByLevel: function (array, topLv, btmLv) {
    var newArr = []
    for (var i = 0; i < array.length; i++) {
      var commentLevel = array[i].commentLevel;
      if (commentLevel <= topLv && commentLevel >= btmLv) {
        newArr.push(array[i]);
      } else {
        continue;
      }
    }
    return newArr;
  },
  onShareAppMessage: function () {
    var that = this;
    var goodId = that.data.goodId;
    return {
      title: goodName,
      path: '/pages/details/comment_details/comment_details?goodId=' + goodId,
      success: function (res) {
        //转发成功
        wx.showToast({
          title: '转发成功',
          icon: 'success'
        })
      },
      fail: function (res) {
        //转发失败
      }
    }
  },
})